package com.schedule.config;

import com.schedule.utils.JwtTokenInterceptorUtil;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;
@Slf4j
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Autowired
    private JwtTokenInterceptorUtil jwtTokenAdminInterceptor;
    /**
     * 注册拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册自定义拦截器...\n{}",jwtTokenAdminInterceptor);
        registry.addInterceptor(jwtTokenAdminInterceptor)
                .addPathPatterns("/admin/**","/schedule/**")
                .excludePathPatterns("/admin/login","/admin/register");
    }
    /*
     * 设置静态资源映射
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("静态资源映射");
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/", "classpath:/public/"); // 可选：处理其他静态资源
    }

    /**
     * 扩展spring mvc框架的消息转换器
     * @param
     */
//    @Override
//    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
//        log.info("扩展消息转换器");
//
//        //创建一个消息转换器
//        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
//        //需要为消息转换器设置一个对象转换器，对象转换器可以将Java对象序列化为json数据
//        converter.setObjectMapper(new JacksonObjectMapper());
//        //将自己的消息转换器加入到容器中
//        converters.add(0, converter);
//    }
    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .info(apiInfo());
    }

    private Info apiInfo() {
        return new Info()
                .title("智能日程管理系统 API 文档")
                .description("基于 Spring Boot + OpenAPI 3 的日程管理后台接口文档")
                .version("3.0");
    }
    @Bean
    public GroupedOpenApi adminApi() {
        log.info("导入管理端接口测试文档");
        return GroupedOpenApi.builder()
                .group("用户操作") // 分组名称
                .packagesToScan("com.schedule.controller.admin") // 匹配哪些路径下的接口
                .build();
    }

    @Bean
    public GroupedOpenApi userApi() {
        log.info("导入用户端接口测试文档");
        return GroupedOpenApi.builder()
                .group("日志操作") // 分组名称
                .packagesToScan("com.schedule.controller.user") // 匹配哪些路径下的接口
                .build();
    }


}
